# Boyoon Lee "The Impact of Educational Content on Anti-Immigrant Attitudes"
# 3. Online Appendix C (Table C.2, Table C.3, Table C.4)
# Last updated: 2022-11-16


# Initial settings --------------------------------------------------------

# Set directory
setwd("Your Path Here")

# Packages
library(tidyr)
library(dplyr)
library(miceadds) # for lm.cluster
library(stargazer)


# Load data  --------------------------------------------------------------

### Original data
data <- read.csv("./cleaned_TSCS_did.csv", header=TRUE, sep=",")


### Matched data
# Data set saved from Appendix D (Matching) results.
# See file "4_Online_Appendix_D.R" for the construction
load("./matched_data_Ver3_5_1.Rda")


# Recode / Re-class  ------------------------------------------------------

### Make date variable using Birth year and Birth month
# Since there is no date of birth available, I set the date to the first day of the month
data$date <- as.Date(with(data, paste(birth_yr, birth_month, "01", sep="-")), "%Y-%m-%d")

### Only leave school cohorts before 2002 
# In 2001, Knowing Taiwan series discontinued and replaced with the New Grade 1-9 curriculum (9 year curriculum)
# In addition, from 1968, junior vocational school shut down and expanded senior vocational school
data<-subset(data, data$school_yr<2002&data$school_yr>1967)


# Subsets for the Comparison Group ---------------------------------------

# Main comparison group 
yr13_22<-subset(data,school_yr>1987 & school_yr<2002)
yr13_22<-subset(yr13_22,edu_level>3) # those who graduated from at least junior high schools




#######################################################
##         [APPENDIX C] Summary statistics           ##
#######################################################


# Table C.2  -------------------------------------------------------------

### Entire sample in the dataset
all<-yr13_22[c("num_imm","group","post","female","married","employed","social_class","edu_uni","ba","prts_gen")]
stargazer(all,
          title="Summary Statistics for Selected variables",
          type="text",digit=2,
          iqr=TRUE,
          covariate.labels = c("Preferred Num. of Immigrants","Education Path (general = 1)","Post (post reform = 1)",
                               "Female (female = 1)","Married (married = 1)","Employed (employed = 1)","Self-evaluated social class",
                               "Higher education (high = 1)","Bachelor's degree (BA = 1)",
                               "Parent's path (general - 1)"))


### Sample used in Table 2 Model (1)
# Estimate the main model first
did.cl.c2 <- lm.cluster(data= yr13_22, 
                        formula= num_imm ~ post + group + TG
                        + female + married + employed + social_class + edu_uni
                        + post*prts_gen 
                        + as.factor(edu_level) + as.factor(loc_birth) + as.factor(school_yr) + as.factor(year), 
                        cluster = as.integer(yr13_22$school_yr)*as.integer(yr13_22$group_plus))
# Then calculate the summary statics based on the sample used for the estimation
c2.data<-did.cl.c2$lm_res$model
main.sample<-c2.data[c("num_imm","group","post","female","married","employed","social_class","edu_uni","prts_gen")]
stargazer(main.sample,
          title="Summary Statistics for Selected variables",
          type="text",digit=2,
          iqr=TRUE,
          covariate.labels = c("Preferred Num. of Immigrants","Education Path (general = 1)","Post (post reform = 1)",
                               "Female (female = 1)","Married (married = 1)","Employed (employed = 1)","Self-evaluated social class",
                               "Higher education (high = 1)",
                               "Parent's path (general - 1)"))



# Table C.3  -------------------------------------------------------------

### Post-Renshi Taiwan cohort
post<-yr13_22 %>% filter(post==1) %>% 
  select(num_imm,group,post,female,married,employed,social_class,edu_uni,ba,prts_gen)
post<-post[c("num_imm","group","female","married","employed","social_class","edu_uni","ba","prts_gen")]
stargazer(post,
          title="Summary Statistics for Selected variables (Post-Renshi Taiwan)",
          type="text",digit=2,
          iqr=TRUE,
          covariate.labels = c("Preferred Num. of Immigrants",
                               "Education Path (general = 1)",
                               "Female (female = 1)",
                               "Married (married = 1)",
                               "Employed (employed = 1)",
                               "Self-evaluated social class",
                               "Higher education (high = 1)",
                               "Bachelor's degree (BA = 1)",
                               "Parent's path (general = 1)"))

### Pre-Renshi Taiwan cohort
pre<-yr13_22 %>% filter(post==0) %>% 
  select(num_imm,group,post,female,married,employed,social_class,edu_uni,ba,prts_gen)
pre<-pre[c("num_imm","group","female","married","employed","social_class","edu_uni","ba","prts_gen")]
stargazer(pre,
          title="Summary Statistics for Selected variables (Pre-Renshi Taiwan)",
          type="text",digit=2,
          iqr=TRUE,
          covariate.labels = c("Preferred Num. of Immigrants",
                               "Education Path (general = 1)",
                               "Female (female = 1)",
                               "Married (married = 1)",
                               "Employed (employed = 1)",
                               "Self-evaluated social class",
                               "Higher education (high = 1)",
                               "Bachelor's degree (BA = 1)",
                               "Parent's path (general - 1)"))


# Table C.4  -------------------------------------------------------------

### Original Sample used in Table 2 Model (1)

# Vocational path: Post-Renshi Taiwan cohort
post_vs<-c2.data %>% filter(post==1 & group==0) %>% 
  select(num_imm,group,post,female,married,employed,social_class,edu_uni,prts_gen)
post_vs<-post_vs[c("num_imm","female","married","employed","social_class","edu_uni","prts_gen")]
stargazer(post_vs,
          type="text",digit=2,
          summary.stat=c("n","mean","sd"),
          covariate.labels = c("Preferred Num. of Immigrants",
                               "Female (female = 1)",
                               "Married (married = 1)",
                               "Employed (employed = 1)",
                               "Self-evaluated social class",
                               "Higher education (high = 1)",
                               "Parent's path (general = 1)"))
# Vocational path: Pre-Renshi Taiwan cohort
pre_vs<-c2.data %>% filter(post==0 & group==0) %>% 
  select(num_imm,group,post,female,married,employed,social_class,edu_uni,prts_gen)
pre_vs<-pre_vs[c("num_imm","female","married","employed","social_class","edu_uni","prts_gen")]
stargazer(pre_vs,
          type="text",digit=2,
          summary.stat=c("n","mean","sd"),
          covariate.labels = c("Preferred Num. of Immigrants",
                               "Female (female = 1)",
                               "Married (married = 1)",
                               "Employed (employed = 1)",
                               "Self-evaluated social class",
                               "Higher education (high = 1)",
                               "Parent's path (general = 1)"))

# Academic path: Post-Renshi Taiwan cohort
post_as<-c2.data %>% filter(post==1 & group==1) %>% 
  select(num_imm,group,post,female,married,employed,social_class,edu_uni,prts_gen)
post_as<-post_as[c("num_imm","female","married","employed","social_class","edu_uni","prts_gen")]
stargazer(post_as,
          type="text",digit=2,
          summary.stat=c("n","mean","sd"),
          covariate.labels = c("Preferred Num. of Immigrants",
                               "Female (female = 1)",
                               "Married (married = 1)",
                               "Employed (employed = 1)",
                               "Self-evaluated social class",
                               "Higher education (high = 1)",
                               "Parent's path (general = 1)"))
# Academic path: Pre-Renshi Taiwan cohort
pre_as<-c2.data %>% filter(post==0 & group==1) %>% 
  select(num_imm,group,post,female,married,employed,social_class,edu_uni,prts_gen)
pre_as<-pre_as[c("num_imm","female","married","employed","social_class","edu_uni","prts_gen")]
stargazer(pre_as,
          type="text",digit=2,
          summary.stat=c("n","mean","sd"),
          covariate.labels = c("Preferred Num. of Immigrants",
                               "Female (female = 1)",
                               "Married (married = 1)",
                               "Employed (employed = 1)",
                               "Self-evaluated social class",
                               "Higher education (high = 1)",
                               "Parent's path (general = 1)"))



### Matched sample used in Table 2 Model (2)

# Vocational path: Post-Renshi Taiwan cohort
post_vm<-dta_m %>% filter(post==1 & group=="Control") %>% 
  select(num_imm,group,post,female,married,employed,social_class,edu_uni,prts_gen)
post_vm<-post_vm[c("num_imm","female","married","employed","social_class","edu_uni","prts_gen")]
stargazer(post_vm,
          type="text",digit=2,
          summary.stat=c("n","mean","sd"),
          covariate.labels = c("Preferred Num. of Immigrants",
                               "Female (female = 1)",
                               "Married (married = 1)",
                               "Employed (employed = 1)",
                               "Self-evaluated social class",
                               "Higher education (high = 1)",
                               "Parent's path (general = 1)"))
# Vocational path: Pre-Renshi Taiwan cohort
pre_vm<-dta_m %>% filter(post==0 & group=="Control") %>% 
  select(num_imm,group,post,female,married,employed,social_class,edu_uni,prts_gen)
pre_vm<-pre_vm[c("num_imm","female","married","employed","social_class","edu_uni","prts_gen")]
stargazer(pre_vm,
          type="text",digit=2,
          summary.stat=c("n","mean","sd"),
          covariate.labels = c("Preferred Num. of Immigrants",
                               "Female (female = 1)",
                               "Married (married = 1)",
                               "Employed (employed = 1)",
                               "Self-evaluated social class",
                               "Higher education (high = 1)",
                               "Parent's path (general = 1)"))

# Academic path: Post-Renshi Taiwan cohort
post_am<-dta_m %>% filter(post==1 & group=="Treatment") %>% 
  select(num_imm,group,post,female,married,employed,social_class,edu_uni,prts_gen)
post_am<-post_am[c("num_imm","female","married","employed","social_class","edu_uni","prts_gen")]
stargazer(post_am,
          type="text",digit=2,
          summary.stat=c("n","mean","sd"),
          covariate.labels = c("Preferred Num. of Immigrants",
                               "Female (female = 1)",
                               "Married (married = 1)",
                               "Employed (employed = 1)",
                               "Self-evaluated social class",
                               "Higher education (high = 1)",
                               "Parent's path (general = 1)"))
# Academic path: Pre-Renshi Taiwan cohort
pre_am<-dta_m %>% filter(post==0 & group=="Treatment") %>% 
  select(num_imm,group,post,female,married,employed,social_class,edu_uni,prts_gen)
pre_am<-pre_am[c("num_imm","female","married","employed","social_class","edu_uni","prts_gen")]
stargazer(pre_am,
          type="text",digit=2,
          summary.stat=c("n","mean","sd"),
          covariate.labels = c("Preferred Num. of Immigrants",
                               "Female (female = 1)",
                               "Married (married = 1)",
                               "Employed (employed = 1)",
                               "Self-evaluated social class",
                               "Higher education (high = 1)",
                               "Parent's path (general = 1)"))

